<!DOCTYPE HTML>
<script src="../../../../resources/js-test.js"></script>
<style>
#target {
  width: 50px;
  height: 50px;
}
</style>
<iframe id="target" src="resources/event-delegator.html"></iframe>
<div id=console></div>
<script>
var scrollbarEventType;

function onEventInFrame(e) {
    debug("Received " + e.type + " in child frame");
    if (e.type == scrollbarEventType) {
        debug('Adding scrollbars to iframe');
        target.contentDocument.body.style.height = "500px";
    }
}

function eventLogger(e) {
    debug("Received " + e.type + " in main frame");
}

document.addEventListener('mousemove', eventLogger);
document.addEventListener('mousedown', eventLogger);
document.addEventListener('mouseup', eventLogger);
document.addEventListener('click', eventLogger);

description("Taps at a point that becomes a scrollbar (i.e. hit test with innerNode=null) during the tap and verifies we don't crash.");

// Tap at the right edge of the frame, where its scrollbar would live.
var rect = target.getBoundingClientRect();
var point = {
    x: rect.right - 4,
    y: rect.top + rect.height / 2
};

function doTap(type)
{
    return new Promise(function(resolve, reject) {
        if (type) {
            debug('Test case: Add scrollbars during ' + type);
            scrollbarEventType = type;
            target.contentDocument.body.style.height = "0";
        } else {
            debug('Test case: Tap on consistent scrollbar');
        }

        eventSender.gestureTapDown(point.x, point.y);
        eventSender.gestureShowPress(point.x, point.y);
        debug("Sending GestureTap");
        eventSender.gestureTap(point.x, point.y);

        setTimeout(function() {
            debug('');
            resolve();
        }, 0);
    });
}

if (window.eventSender) {
    jsTestIsAsync = true;

    target.onload = function() {
        doTap()
        .then(function() { return doTap('mousemove'); })
        .then(function() { return doTap('mousedown'); })
        .then(function() { return doTap('mouseup'); })
        .catch(function(err) {
            testFailed("Promise rejected: " + err.message);
        }).then(finishJSTest);
    }
} else {
    debug("This test requires eventSender");
}
</script>
